package com.imu.purse.modules.trade.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.imu.common.exception.RRException;
import com.imu.core.base.BaseService;
import com.imu.datasources.DataSourceNames;
import com.imu.datasources.annotation.DataSource;
import com.imu.enums.OrderTypeStatus;
import com.imu.enums.PStatus;
import com.imu.purse.modules.app.entity.ImuUserEntity;
import com.imu.purse.modules.app.service.ImuUserService;
import com.imu.purse.modules.trade.dao.TradeLogMapper;
import com.imu.purse.modules.trade.entity.FundEntity;
import com.imu.purse.modules.trade.entity.TradeInfoEntity;
import com.imu.purse.modules.trade.entity.TradeLogEntity;
import com.imu.purse.modules.trade.service.FundService;
import com.imu.purse.modules.trade.service.TradeInfoService;
import com.imu.purse.modules.trade.service.TradeLogService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 交易流水
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-03-22 10:33:21
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class TradeLogServiceImpl extends BaseService<TradeLogEntity> implements TradeLogService 
{
	@Override
    @Transactional
    public int save(final TradeLogEntity tradeLog)
	{
    	return super.insert(tradeLog);
	}
	
	@Override
	@Transactional
    public int update(final TradeLogEntity tradeLog) 
    {
    	return mapper.updateByPrimaryKeySelective(tradeLog);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final TradeLogEntity tradeLog)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(tradeLog)))
    	{
        	len = this.insert(tradeLog);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
    
    @Override
    @DataSource(name=DataSourceNames.SECOND)
    public TradeLogEntity findById(final Long id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(id);
    }
    
    @Override
    @DataSource(name=DataSourceNames.SECOND)
    public List<TradeLogEntity> queryList(final TradeLogEntity tradeLog)
    {
    	if (tradeLog.getPage() != null && tradeLog.getLimit() != null)
    	{
            PageHelper.startPage(tradeLog.getPage(), tradeLog.getLimit());
        }
    	
    	return mapper.findByPage(tradeLog);
    }
    
    @Override
	@Transactional
    public void transfer(final TradeLogEntity entity)
    {
    	this.save(entity); //保存流水信息
    	
    	//验证平台收款用户信息
    	ImuUserEntity user = userService.queryByMobile(entity.getRecAccount());
    	if(null == user || StrUtil.isBlank(user.getRealName()) || !user.getRealName().equals(entity.getRecName()))
    	{
    		throw new RRException("收款用户信息有误", 1100);
    	}
    	
    	//更新用户钱包
    	FundEntity fEntity = new FundEntity();
    	fEntity.setId(user.getId());
    	fEntity.setMoney(entity.getMoney());
    	fEntity.setTimeUpdate(new Date());
    	fundService.upMoney(fEntity);
    	
    	//保存转账记录
    	TradeInfoEntity info = new TradeInfoEntity();
    	info.setRecAddr(entity.getRecAccount());
    	info.setMoney(entity.getMoney());
    	info.setOrderNo(entity.getOrderNo());
    	info.setDetail(entity.getDetail());
    	info.setPayUserId(user.getId());
    	info.setRecUserId(user.getId());
    	info.setOrderState(PStatus.Finished.getType());
    	info.setOrderType(OrderTypeStatus.IN.getType());
    	tradeInfoService.save(info);
    	
    	//更新支持状态
    	TradeLogEntity dto = new TradeLogEntity();
    	dto.setId(entity.getId());
        dto.setOrderState(PStatus.Finished.getType());
        this.update(dto);
    }
    
	@Autowired
	private TradeLogMapper mapper;
	
	@Autowired
	private ImuUserService userService;
	
	@Autowired
	private TradeInfoService tradeInfoService;
	
	@Autowired
	private FundService fundService;
}
